package com.lczc.soa.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lczc.soa.domain.Street;
import com.lczc.soa.domain.SearchStreetResponse;
import com.lczc.soa.domain.Street;
import com.lczc.soa.mapper.StreetMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StreetServiceImpl implements StreetService {
    @Autowired
    private StreetMapper streetMapper;

    @Override
    public int saveStreet(Street street) {
        if(StringUtils.isNotBlank(street.getStreetId())){
            return streetMapper.updateByPrimaryKey(street);
        }else {
            return streetMapper.insert(street);
        }
    }

    @Override
    public SearchStreetResponse findAllStreet(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
//        List<Street> streets = streetMapper.selectByExample(new StreetExample());
        List<Street> streets = streetMapper.selectAll();
//        for(Street street : streets){
//            System.out.println(street.getArea().getAreaName());
//        }
        return new SearchStreetResponse(((Page)streets).getTotal(),streets);
    }

    @Override
    public SearchStreetResponse findStreetLikeName(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Street> streets = streetMapper.selectByLikeName(keywords);
        return new SearchStreetResponse(((Page)streets).getTotal(),streets);
    }

    @Override
    public Street findStreetByPrimaryKey(String id) {
        return streetMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteStreet(String id) {
        return streetMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Street> listStreet() { return streetMapper.selectAll(); }

    @Override
    public List<Street> findStreetByArea(String id) {
        return streetMapper.selectByArea(id);
    }

    @Override
    public Street findStreetByDoor(String id) {
        return streetMapper.selectByDoor(id);
    }
}
